package com.sam.im.heiqun.uis.fragments;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.CircleNums;
import com.sam.im.heiqun.entities.ImGroupEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.activities.CircleActivity;
import com.sam.im.heiqun.uis.activities.CommonScanActivity;
import com.sam.im.heiqun.uis.activities.FriendDetailActivity;
import com.sam.im.heiqun.uis.activities.GlobalRoamingActivity;
import com.sam.im.heiqun.uis.activities.GroupDetailsActivity;
import com.sam.im.heiqun.uis.activities.MyWebViewManageActivity;
import com.sam.im.heiqun.uis.activities.NearbyActivity;
import com.sam.im.heiqun.uis.activities.PayActivity;
import com.sam.im.heiqun.uis.activities.ShakeActivity;
import com.yuyh.library.utils.EventBusType;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.sam.im.heiqun.zxing.decode.DecodeThread;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.fragments.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by leijiawen on 2017/6/11.
 */
public class FindFragment extends BaseFragment {
    @BindView(R.id.circle_layout)
    RelativeLayout circleLayout;
    @BindView(R.id.yaoyiyao_layout)
    LinearLayout yaoyiyao ;
    @BindView(R.id.nearby_layout)
    LinearLayout nearby ;
    @BindView(R.id.scan_layout)
    LinearLayout scan ;
    @BindView(R.id.layout_look)
    LinearLayout layout_look ;
    @BindView(R.id.layout_search)
    LinearLayout layout_search ;
    @BindView(R.id.layout_small_project)
    LinearLayout layout_small_project ;
    @BindView(R.id.find_num)
    TextView find_num ;
    @BindView(R.id.find_news)
    TextView find_news ;
    @BindView(R.id.img_friend)
    ImageView imgFriend ;
    @BindView(R.id.view_top)
    View view_top ;
    @Override
    public int getContentViewId() {
        return R.layout.layout_findfragment;
    }

    @Override
    public void init(Bundle savedInstanceState) {

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);//订阅
        }

        List<CircleNums> circleNumses = CircleNums.find(CircleNums.class, "unicid=? and isread=? and type!=?", new String[]{ToolsUtils.getMyUserId(),"0", Constant.MESSAGE_TYPR_CIRCLE+""});
        List<CircleNums> circleNumses2 = CircleNums.find(CircleNums.class, "unicid=? and isread=? and type=?", new String[]{ToolsUtils.getMyUserId(),"0",Constant.MESSAGE_TYPR_CIRCLE+""});
        if (circleNumses != null&&circleNumses.size()!=0) {
            find_num.setText(circleNumses.size()+"");
            find_num.setVisibility(View.VISIBLE);
//            find_news.setVisibility(View.GONE);
//            imgFriend.setVisibility(View.GONE);
            List<CircleNums> circleNumses3 = CircleNums.find(CircleNums.class, "unicid=? and isread=? and type=?", new String[]{ToolsUtils.getMyUserId(),"0", Constant.MESSAGE_TYPR_CALL_ICE+""});
            if (null!=circleNumses3 && circleNumses3.size() > 0){
                CircleNums circleNums = circleNumses3.get(circleNumses3.size() - 1);
                String fromuserimgurl = circleNums.getFromuserimgurl();
                Glide.with(getActivity()).load(fromuserimgurl).centerCrop().into(imgFriend);
                imgFriend.setVisibility(View.VISIBLE);
            }
        }else if (circleNumses2.size()>0){
            CircleNums circleNums = circleNumses2.get(circleNumses2.size() - 1);
            String fromuserimgurl = circleNums.getFromuserimgurl();
            Glide.with(getActivity()).load(fromuserimgurl).centerCrop().into(imgFriend);
            imgFriend.setVisibility(View.VISIBLE);
            find_num.setVisibility(View.GONE);
            find_news.setVisibility(View.VISIBLE);
        }else {
            find_num.setVisibility(View.GONE);
            find_news.setVisibility(View.GONE);
            imgFriend.setVisibility(View.GONE);
        }



        String isShowLook = ToolsUtils.saveLook(getActivity(),ToolsUtils.getMyUserId(),"1",2);
        if ("1".equals(isShowLook)){
            layout_look.setVisibility(View.VISIBLE);
            view_top.setVisibility(View.VISIBLE);
        }
        String isShowSearch = ToolsUtils.saveSearch(getActivity(),ToolsUtils.getMyUserId(),"1",2);
        if ("1".equals(isShowSearch)){
            layout_search.setVisibility(View.VISIBLE);
            view_top.setVisibility(View.VISIBLE);
        }


    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行 接收到消息进行处理
    public void onAiteEvent(Integer string) {
        if (string == EventBusType.REFRESH_FRAGMENT_FIND){
            int i = 0 ;
            String isShowLook = ToolsUtils.saveLook(getActivity(),ToolsUtils.getMyUserId(),"1",2);
            if ("1".equals(isShowLook)){
                layout_look.setVisibility(View.VISIBLE);
                view_top.setVisibility(View.VISIBLE);
            }else {
                i++;
                layout_look.setVisibility(View.GONE);
            }
            String isShowSearch = ToolsUtils.saveSearch(getActivity(),ToolsUtils.getMyUserId(),"1",2);
            if ("1".equals(isShowSearch)){
                layout_search.setVisibility(View.VISIBLE);
                view_top.setVisibility(View.VISIBLE);
            }else {
                i++;
                layout_search.setVisibility(View.GONE);
            }
            if (2 == i){
                view_top.setVisibility(View.GONE);
            }
        }

    }

    public void  setCircleNum (int nums,int type){ // 1 动态   2  赞和评论
        switch (type){
            case 1:
                if (find_num.getVisibility()==View.VISIBLE){
                    return;
                }else {
                    if (nums!=0) {
                        List<CircleNums> circleNumses2 = CircleNums.find(CircleNums.class, "unicid=? and isread=? and type=?", new String[]{ToolsUtils.getMyUserId(), "0", Constant.MESSAGE_TYPR_CIRCLE + ""});
                        if (circleNumses2.size() > 0) {
                            CircleNums circleNums = circleNumses2.get(circleNumses2.size() - 1);
                            String fromuserimgurl = circleNums.getFromuserimgurl();
                            Glide.with(getActivity()).load(fromuserimgurl).centerCrop().into(imgFriend);
                            imgFriend.setVisibility(View.VISIBLE);
                            find_news.setVisibility(View.VISIBLE);
                        } else {
                            imgFriend.setVisibility(View.GONE);
                            find_news.setVisibility(View.GONE);
                        }
                    } else {
                        imgFriend.setVisibility(View.GONE);
                        find_news.setVisibility(View.GONE);
                        }
                }
                break;
            case 2:
                find_news.setVisibility(View.GONE);
                imgFriend.setVisibility(View.GONE);
                if (nums!=0){
                    find_num.setVisibility(View.VISIBLE);
                    find_num.setText(nums+"");
                }else {
                    find_num.setVisibility(View.GONE);
                }
                break;
        }
        if (find_num != null) {

        }
    }

    @OnClick({R.id.circle_layout,R.id.yaoyiyao_layout,R.id.nearby_layout,R.id.scan_layout,R.id.layout_look,
            R.id.layout_search, R.id.layout_small_project})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.circle_layout:
                startActivity(CircleActivity.class);
                break;
            case R.id.yaoyiyao_layout:
                startActivity(ShakeActivity.class);
                break;
            case R.id.nearby_layout:
                startActivity(NearbyActivity.class);
//                GlobalRoamingActivity.start(getActivity());//全球漫游
                break;
            case R.id.scan_layout:
                Intent intent = new Intent(getActivity(),CommonScanActivity.class);
                intent.putExtra(Constant.REQUEST_SCAN_MODE, DecodeThread.QRCODE_MODE);
                startActivityForResult(intent,333);
                //startActivity(intent);
                break;
            case R.id.layout_look:
//                showToast("敬请期待！");
                MyWebViewManageActivity.start(getActivity(), 0, "http://samim.huiwork.com/look");
                break;
            case R.id.layout_search:
                showToast("敬请期待！");
                break;
            case R.id.layout_small_project:
//                showToast("敬请期待！");
                MyWebViewManageActivity.start(getActivity(), 2, "https://www.hq12345678.com/imgroup/html/index.html");
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 333:
                if (resultCode== Constant.REQUEST_SCAN_MODE_BARCODE_MODE){
                    //String money = data.getStringExtra("money");
                    String str = data.getStringExtra("destid");
                    String destid = str.substring(2);
                    String typeStr = str.substring(0, 1);
                    if (typeStr.equals("a")){
                        Bundle bundle = new Bundle();
                        bundle.putLong("id", Long.parseLong(destid));
                        bundle.putInt("type", 0);
                        startActivity(FriendDetailActivity.class, bundle);
                    }else if (typeStr.equals("b")){
                        String fromid = ToolsUtils.getMyUserId();
                        PGService.getInstance().getGroupById(destid, fromid).subscribe(new AbsAPICallback<ImGroupEntivity>() {
                            @Override
                            public void onNext(ImGroupEntivity groupEntivity) {
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("groupSeri",groupEntivity);
                                bundle.putLong("groupid", groupEntivity.getId());
                                startActivity(GroupDetailsActivity.class,bundle);
                            }

                            @Override
                            protected void onResultError(ApiException ex) {
                                ToolsUtils.showToast(getActivity(),getActivity().getResources().getString(R.string.net_error));
                            }
                        });

                    } else if (typeStr.equals("c")){
                        Intent payintent = new Intent(getActivity(), PayActivity.class);
                        payintent.putExtra("money",destid);
                        startActivity(payintent);
                    }else if (typeStr.equals("d")){
                        Intent payintent = new Intent(getActivity(), PayActivity.class);
                        payintent.putExtra("paymoney",destid);
                        startActivity(payintent);
                    }else if (str.contains("http")){
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(str);
                        intent.setData(content_url);
                        startActivity(intent);
                    }else {
                        showToast(getActivity().getResources().getString(R.string.no_konw_qrcode));
                    }
                   /* try {
                        GetmoneyEntivity getmoneyEntivity = new Gson().fromJson(destid, GetmoneyEntivity.class);

                    }catch (Exception e){


                    }*/
                    //GetmoneyEntivity getmoneyEntivity = new Gson().fromJson(money, GetmoneyEntivity.class);
                    //Log.i("info","扫描结果=="+destid);
                }
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);//解除订阅
    }
}
